--[[

Modified by yleaf (yaroot@gmail.com)
Credits to Yrys


ChatLink.lua
by Yrys - Hellscream <http://twpa.net/contact.php>

ChatLink enables linking items, enchants, spells, talents, quests, and achievements in private chat channels. ChatLink by default will not work in public channels such as General or Trade to avoid undue spam. Also, note that other people will need this addon to see your links in chat channels correctly.


Version history:
- 1.4.2.30100 (2009-04-18): Updated for 3.1.0 patch; added Chinese localizations (thanks adamhj!).
- 1.4.1.30000 (2008-11-25): Swapped back to the newer item link format.  Also fixed a possible bug with profession links.  Thanks to Sylvanaar for pointing it out.
- 1.4.0.30000 (2008-09-16): Updated for 3.0.2 patch.  Fixed item and talent links; added profession and achievement links.
- 1.3.0.20400 (2008-04-07): Updated for 2.4.0 patch.  Added spell, talent and quest linking support.
- 1.2.0.20300 (2008-03-08): Updated for 2.3.0 patch.
- 1.2.0.20200 (2007-09-27): Updated for 2.2.0 patch.
- 1.2.0.20100 (2007-04-28): Verified recipe link support.  Added a warning message when other chatlink-type addons are detected, and another warning if a newer ChatLink link style is detected.
- 1.1.4.20003 (2007-01-17): More Burning Crusade fixes (itemIDs can now also be negative).
- 1.1.3.20003 (2007-01-14): Updated for 2.0.3 patch.
- 1.1.3.20000 (2006-11-11): Compatibility fixes for Burning Crusade and UI 2.0.
- 1.1.3.11200 (2006-11-05): Added the guild recruitment channel to the excluded channels.
- 1.1.2.11200 (2006-09-29): Re-added compatibility with links before 1.1.0, and added future compatibility as well.
- 1.1.0.11200 (2006-09-27): Updated for 1.12 patch, and added enchant link support.
- 1.0.4.11100 (2006-07-24): Updated for 1.11 patch.
- 1.0.4.11000 (2006-03-17): Updated for 1.10 patch.
- 1.0.4.10900 (2005-12-02): Updated for 1.9 patch.
- 1.0.4.1800  (2005-09-30): Updated for 1.8 patch; now with French localization!  Thanks Viny!
- 1.0.3.1700  (2005-09-07): Updated for 1.7 patch; added German localization. Thanks Getch!
- 1.0.2.1600  (2005-07-12): Updated for 1.6 patch.
- 1.0.2.1500  (2005-06-07): Updated for 1.5 patch.
- 1.0.2.1300  (2005-04-14): Fixed a bug with nil strings in AddMessage.
- 1.0.1.1300  (2005-04-06): Added support for links in other chat windows.
- 1.0.0.1300  (2005-04-05): Initial release.


To addon developers:
Feel free to use this code.  I would appreciate it if you let me know and/or give credit.  (It's a bummer to have someone take your work and claim it as their own.)  If you're looking to make an addon compatible with ChatLink links, here's the current formatting.

Items: {CLINK:item:<color in hex>:<item ID and item data - 9 fields>:<item name>}
Enchant recipes: {CLINK:enchant:<color in hex>:<enchant ID>:<enchant name>}
Spells: {CLINK:spell:<color in hex>:<spell ID>:<spell name>}
Quests: {CLINK:quest:<color in hex>:<quest ID>:<quest level>:<quest name>}
Professions: {CLINK:trade:<profession ID>:<current skill>:<max skill>:<player id?>:<recipe hash>:<profession name>}
Talents: {CLINK:talent:<talent ID>:<points in talent - 2>:<talent name>}
Achievements: {CLINK:achievement:<achievement ID>:<player id?>:<have it?>:<month>:<day>:<year>:<4 additional fields>:<achievement name>}
Legacy item links (pre ChatLink 1.1.0): {CLINK:<color in hex>:<item ID and item data - 9 fields>:<item name>}

Notes:
- Items: Items had an extra field added in WotLK and are up to 9 data fields.
- Legacy item links: Versions of ChatLink before 1.4.1 (and possibly other chat link addons) sent item links using the legacy format.  You may want to capture and decode both item link formats for compatibility.
- Quests: You pretty much have to discard the color you get when decoding quest CLINKs.  Since the person sending the quest may not be the same level as you, the quest won't have the same color for you as for them.  See ChatLink_SetQuestLinkColor below.

Send me an e-mail if you have any questions!
-Yrys

]]

local loc = GetLocale()
local GChannels = loc == 'deDE' and {
	'^Allgemein',
	'^Handel',
	'^SucheNachGruppe',
	'^LokaleVerteidigung',
	'^WeltVerteidigung',
	'^Gildenrekrutierung',
} or loc == 'frFR' and {
	'^Général',
	'^Commerce',
	'^RechercheGroupe',
	'^DéfenseLocale',
	'^DéfenseUniverselle',
	'^RecrutementDeGuilde',
} or loc == 'zhTW' and {
	'^綜合',
	'^交易',
	'^尋求組隊',
	'^本地防務',
	'^世界防務',
	'^公會招募',
} or loc == 'zhCN' and {
	'^综合',
	'^交易',
	'^寻求组队',
	'^本地防务',
	'^世界防务',
	'^工会招募',
} or {
	'^General',
	'^Trade',
	'^LookingForGroup',
	'^LocalDefense',
	'^WorldDefense',
	'^GuildRecruitment',
}

local CHATLINK_VER = '1.4.2.30100'

-- Get color for quest level -- based off Blizzard's code in FrameXML/QuestLogFrame.lua
local function ChatLink_GetQuestDifficultyColor(level)
	local c = GetQuestDifficultyColor(level)
	return format('%02x%02x%02x', c.r, c.g, c.b)
end

-- Update the color of a quest link based on player level.
local function ChatLink_SetQuestLinkColor(questId, questLevel, questName)
	if questId and questLevel and questName
	then
		local newQuestColor = ChatLink_GetQuestDifficultyColor(questLevel)
		return '|cff' .. newQuestColor .. '|Hquest:' .. questId .. ':' .. questLevel .. '|h[' .. questName .. ']|h|r'
	else
		return nil
	end
end

-- Turn CLINKs into normal item and enchant links.
local function Decompose(chatstring)
	if chatstring then
		chatstring = gsub(chatstring, '{CLINK:item:(%x+):(%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-):([^}]-)}', '|c%1|Hitem:%2|h[%3]|h|r')
		chatstring = gsub(chatstring, '{CLINK:enchant:(%x+):(%-?%d-):([^}]-)}', '|c%1|Henchant:%2|h[%3]|h|r')
		chatstring = gsub(chatstring, '{CLINK:quest:%x+:(%-?%d-):(%-?%d-):([^}]-)}', ChatLink_SetQuestLinkColor)
		chatstring = gsub(chatstring, '{CLINK:spell:(%x+):(%-?%d-):([^}]-)}', '|c%1|Hspell:%2|h[%3]|h|r')
		chatstring = gsub(chatstring, '{CLINK:talent:(%x+):(%-?%d-:%-?%d-):([^}]-)}', '|c%1|Htalent:%2|h[%3]|h|r')
		chatstring = gsub(chatstring, '{CLINK:achievement:(%x+):(%-?%d-:%x-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-):([^}]-)}', '|c%1|Hachievement:%2|h[%3]|h|r')
		chatstring = gsub(chatstring, '{CLINK:trade:(%x+):(%-?%d-:%-?%d-:%-?%d-:%x-:[^}:]-):([^}]-)}','|c%1|Htrade:%2|h[%3]|h|r')

		-- For backward compatibility with older item links.
		chatstring = gsub(chatstring, '{CLINK:(%x+):(%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-):([^}]-)}', '|c%1|Hitem:%2|h[%3]|h|r')

		-- Forward compatibility, for future clink structure changes.
		chatstring = gsub(chatstring, '{CLINK(%d):%[?([^:}%]]-)%]?:([^:}]-)[^}]-}', '%2')
	end
	return chatstring
end

-- Turn item and enchant links into CLINKs.
local function Compose(chatstring)
	if chatstring then
--		1.10 item links: reactivated again.
		chatstring = gsub(chatstring, '|c(%x+)|H(item):(%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-)|h%[([^%]]-)%]|h|r', '{CLINK:%2:%1:%3:%4}')
--		Old item links: disabled, may remove soon.
--		chatstring = gsub (chatstring, '|c(%x+)|Hitem:(%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-)|h%[([^%]]-)%]|h|r', '{CLINK:%1:%2:%3}')
		chatstring = gsub(chatstring, '|c(%x+)|H(enchant):(%-?%d-)|h%[([^%]]-)%]|h|r', '{CLINK:%2:%1:%3:%4}')
		chatstring = gsub(chatstring, '|c(%x+)|H(quest):(%-?%d-:%-?%d-)|h%[([^%]]-)%]|h|r', '{CLINK:%2:%1:%3:%4}')
		chatstring = gsub(chatstring, '|c(%x+)|H(spell):(%-?%d-)|h%[([^%]]-)%]|h|r', '{CLINK:%2:%1:%3:%4}')
		chatstring = gsub(chatstring, '|c(%x+)|H(talent):(%-?%d-:%-?%d-)|h%[([^%]]-)%]|h|r', '{CLINK:%2:%1:%3:%4}')
		chatstring = gsub(chatstring, '|c(%x+)|H(achievement):(%-?%d-:%x-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-)|h%[([^%]]-)%]|h|r', '{CLINK:%2:%1:%3:%4}')
		chatstring = gsub(chatstring, '|c(%x+)|H(trade):(%-?%d-:%-?%d-:%-?%d-:%x-:[^|:]-)|h%[([^%]]-)%]|h|r', '{CLINK:%2:%1:%3:%4}')
	end
	return chatstring
end

local orig_AddMessage = ChatFrame1.AddMessage
local function AddMessage(self, text, ...)
	text = Decompose(text)
	orig_AddMessage(self, text, ...)
end

for i=1, NUM_CHAT_WINDOWS do
	if i~= 2 then
		local cf = _G['ChatFrame'..i]
		cf.AddMessage = AddMessage
	end
end

local function isValidChannel(str)
	for i = 1, #GChannels do
		if strfind(str, GChannels[i]) then
			return false
		end
	end
	return true
end

-- Translate item links into CLINKs on outgoing non-system channel messages.
local SendChatMessage = _G.SendChatMessage
function _G.SendChatMessage(msg, ...)
	local system, _, channel = ...

	if system == 'CHANNEL' then
		local chan_num, chan_name = GetChannelName(channel)
		if chan_name and isValidChannel(chan_name) then
			msg = Compose(msg)
		end
	end

	-- Pass along to original function.
	SendChatMessage(msg, ...)
end
